package com.wasu.sdk_ott.service

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import com.wasu.constant.TAG
import com.wasu.sdk_ott.http.live.IpLiveLoader
import com.wasu.sdk_ott.utils.WasuTvModelUtil

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) 2020 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p> 间隔5分钟循环查询分组频道版本号
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2023/9/4
 */
class LiveVersionService : Service() {

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        Log.v(LiveVersionHelper.VERSION_TAG, "--IpLiveVersionService--onCreate--")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.v(LiveVersionHelper.VERSION_TAG, "--IpLiveVersionService--onStartCommand--")
        queryChannelVersion()
        return START_REDELIVER_INTENT
    }

    /**
     * 开始查询接口，判断版本号
     */
    private fun queryChannelVersion(){
        LiveVersionHelper.startService(this@LiveVersionService)
        if (!WasuTvModelUtil.checkNetworkConnected()){
            Log.e(TAG, "queryChannelVersion, net work disconnect")
            return
        }
        IpLiveLoader().queryChProVersionAsync(
            success = {
                Log.e(TAG, "live version service query channel program version success")
                LiveVersionHelper.analysisLiveVersion(this@LiveVersionService, it)
            }
        )
    }

    override fun onUnbind(intent: Intent?): Boolean {
        return super.onUnbind(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.v(LiveVersionHelper.VERSION_TAG, "--IpLiveVersionService--onDestroy--")
    }
}